// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class AppItem {
    //es6中定义构造函数
    constructor(name = 'winApp', url, icon) {
        this.name = name;
        this.url = url;
        this.icon = icon;
    }
}

window.onload = function() {
    fetch('config.json').then(response => response.json())
        .then(
            config => {
                config.apps.forEach(function(app) {
                    var divItem = $('<div  class="item" url=' + encodeURI(app.url) + '><img class="icon" src="' + app.icon + '" /><span>' + app.name + '</span></div>')
                    $("body").append(divItem);
                });
                var divs = document.querySelectorAll('div');
                for (var i = 0; i < divs.length; i++) {
                    divs[i].addEventListener('click', click);
                }
            })
        .catch(e => console.log("Oops, error", e))
}


function click(e) {
    chrome.tabs.getSelected(function(tabs) {
        var url = e.target.getAttribute("url");
        chrome.tabs.update(tabs.id, { url: url });
    });
}

document.addEventListener('DOMContentLoaded', function() {
    var divs = document.querySelectorAll('div');
    for (var i = 0; i < divs.length; i++) {
        divs[i].addEventListener('click', click);
    }
});